$(function () {

  //点击发表文章 侧边栏视觉效果

  $('.level02 li', window.parent.document).eq(1).addClass('active').siblings().removeClass('active')



  //渲染文章分类
  $.ajax({
    method: 'get',
    url: BigNew.category_list,
    data: {},
    success: (res) => {
      // console.log(res);
      const htmlStr = template('article', { list: res.data })
      $('[name="categoryId"]').html(htmlStr);
    }
  })

  //点击头像 切换头像
  //当数据发生改变，获取图片value值
  $('#inputCover').change(function () {
    const file = $("#inputCover").get(0).files[0];
    const url = URL.createObjectURL(file);
    //console.log(url);
    $('.article_cover').prop('src', url);
  });


  //加载日期插件
  jeDate('#testico', {
    trigger: 'click',
    theme: { bgcolor: "#00A680", pnColor: "#00DDAA" },//绿色主题
    format: "YYYY-MM-DD",
    isinitVal: true,
  });

  /* 2.js初始化选项 */
  tinymce.init({
    selector: '#mytextarea',
    height: '350px',
    language: 'zh_CN',
    directionality: 'ltl',
    browser_spellcheck: true,
    contextmenu: false,
    plugins: [
      "advlist autolink lists link image charmap print preview anchor",
      "searchreplace visualblocks code fullscreen",
      "insertdatetime media table contextmenu paste imagetools wordcount",
      "code"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code",
  });


  //点击 获取fd所以值
  $('[type="submit"]').click(function (e) {
    //阻止默认事件
    e.preventDefault();
    //console.log($(this).attr('data-state'));
    const fd = new FormData(document.querySelector('#form'));
    //获取最新content内容
    const content = tinymce.activeEditor.getContent();
    fd.set('content', content);
    fd.forEach((value, key) => {
      console.log(value, key);
    });

    //发送请求
    $.ajax({
      url: BigNew.article_publish,
      method: 'POST',
      data: fd,
      contentType: false,
      processData: false,
      success: (res) => {
        //console.log(res);
        alert(res.msg);
        location.href = 'article_list.html';
      }
    })
  });
})